//
//  NewsListObj.m
//
//  Created by lizhenqiang  on 16/4/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "NewsListObj.h"


NSString *const kNewsListObjId = @"_id";
NSString *const kNewsListObjRecoImage = @"reco_image";
NSString *const kNewsListObjRemark = @"remark";
NSString *const kNewsListObjTitle = @"title";
NSString *const kNewsListObjPublishTime = @"publish_time";
NSString *const kNewsListObjAbstract = @"abstract";
NSString *const kNewsListObjIsReco = @"is_reco";
NSString *const kNewsListObjCoverImage = @"cover_image";
NSString *const kNewsListObjView = @"views";
NSString *const kNewsListObjisLarge = @"is_large";

@interface NewsListObj ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation NewsListObj

@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize recoImage = _recoImage;
@synthesize remark = _remark;
@synthesize title = _title;
@synthesize publishTime = _publishTime;
@synthesize abstract = _abstract;
@synthesize isReco = _isReco;
@synthesize coverImage = _coverImage;
@synthesize view = _view;
@synthesize isLarge = _isLarge;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.internalBaseClassIdentifier = [self objectOrNilForKey:kNewsListObjId fromDictionary:dict];
            self.recoImage = [self objectOrNilForKey:kNewsListObjRecoImage fromDictionary:dict];
            self.remark = [[self objectOrNilForKey:kNewsListObjRemark fromDictionary:dict] doubleValue];
            self.title = [self objectOrNilForKey:kNewsListObjTitle fromDictionary:dict];
            self.publishTime = [self objectOrNilForKey:kNewsListObjPublishTime fromDictionary:dict];
            self.abstract = [self objectOrNilForKey:kNewsListObjAbstract fromDictionary:dict];
            self.isReco = [[self objectOrNilForKey:kNewsListObjIsReco fromDictionary:dict] boolValue];
            self.coverImage = [self objectOrNilForKey:kNewsListObjCoverImage fromDictionary:dict];
            self.view = [[self objectOrNilForKey:kNewsListObjView fromDictionary:dict] doubleValue];
            self.isLarge = [[self objectOrNilForKey:kNewsListObjisLarge fromDictionary:dict] doubleValue];
        if (self.isLarge) {
           
            self.cellType = KcellTypeBigImage;
            self.imageBili = [NSString getImageBiliFromStr:self.recoImage];
            self.imageName = [NSString getImageUrlFromStr:self.recoImage];
        }
        else
        {
            if ([NSString isEmpty:self.coverImage]) {
                self.cellType = KcellTypeNoImage;
            }
            else
            {
                self.cellType = KcellTypeSmallImage;
                self.imageBili = [NSString getImageBiliFromStr:self.coverImage];
                self.imageName = [NSString getImageUrlFromStr:self.coverImage];
            }
        }

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.internalBaseClassIdentifier forKey:kNewsListObjId];
    [mutableDict setValue:self.recoImage forKey:kNewsListObjRecoImage];
    [mutableDict setValue:[NSNumber numberWithDouble:self.remark] forKey:kNewsListObjRemark];
    [mutableDict setValue:self.title forKey:kNewsListObjTitle];
    [mutableDict setValue:self.publishTime forKey:kNewsListObjPublishTime];
    [mutableDict setValue:self.abstract forKey:kNewsListObjAbstract];
    [mutableDict setValue:[NSNumber numberWithBool:self.isReco] forKey:kNewsListObjIsReco];
    [mutableDict setValue:self.coverImage forKey:kNewsListObjCoverImage];
    [mutableDict setValue:[NSNumber numberWithDouble:self.view] forKey:kNewsListObjView];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.internalBaseClassIdentifier = [aDecoder decodeObjectForKey:kNewsListObjId];
    self.recoImage = [aDecoder decodeObjectForKey:kNewsListObjRecoImage];
    self.remark = [aDecoder decodeDoubleForKey:kNewsListObjRemark];
    self.title = [aDecoder decodeObjectForKey:kNewsListObjTitle];
    self.publishTime = [aDecoder decodeObjectForKey:kNewsListObjPublishTime];
    self.abstract = [aDecoder decodeObjectForKey:kNewsListObjAbstract];
    self.isReco = [aDecoder decodeBoolForKey:kNewsListObjIsReco];
    self.coverImage = [aDecoder decodeObjectForKey:kNewsListObjCoverImage];
    self.view = [aDecoder decodeDoubleForKey:kNewsListObjView];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_internalBaseClassIdentifier forKey:kNewsListObjId];
    [aCoder encodeObject:_recoImage forKey:kNewsListObjRecoImage];
    [aCoder encodeDouble:_remark forKey:kNewsListObjRemark];
    [aCoder encodeObject:_title forKey:kNewsListObjTitle];
    [aCoder encodeObject:_publishTime forKey:kNewsListObjPublishTime];
    [aCoder encodeObject:_abstract forKey:kNewsListObjAbstract];
    [aCoder encodeBool:_isReco forKey:kNewsListObjIsReco];
    [aCoder encodeObject:_coverImage forKey:kNewsListObjCoverImage];
    [aCoder encodeDouble:_view forKey:kNewsListObjView];
}

- (id)copyWithZone:(NSZone *)zone
{
    NewsListObj *copy = [[NewsListObj alloc] init];
    
    if (copy) {

        copy.internalBaseClassIdentifier = [self.internalBaseClassIdentifier copyWithZone:zone];
        copy.recoImage = [self.recoImage copyWithZone:zone];
        copy.remark = self.remark;
        copy.title = [self.title copyWithZone:zone];
        copy.publishTime = [self.publishTime copyWithZone:zone];
        copy.abstract = [self.abstract copyWithZone:zone];
        copy.isReco = self.isReco;
        copy.coverImage = [self.coverImage copyWithZone:zone];
        copy.view = self.view;
    }
    
    return copy;
}


@end
